<?php


namespace App\Http\Controllers\Api;

use App\Businesses\BusinessState;

use App\Services\Business\Models\ThumbsUp;
use App\Services\Member\Interfaces\UserServiceInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class ThumbsUpController extends BaseController
{


    protected $userService;
    protected $businessState;


    public function __construct(
        UserServiceInterface           $userService,
        BusinessState                  $businessState
    )
    {
        $this->userService = $userService;
        $this->businessState = $businessState;

    }

    /**
     * 点赞
     * @param Request $request
     * @param $type
     * @param $id
     * @return JsonResponse
     */
    public function thumbsup(Request $request, $type, $id)
    {
        $modal = 'App\Services\Business\Models\\' . ucfirst($type);
        $modal = new $modal;
        $resource = $modal::find($id);
        $userId = Auth::id();

        $thumbsup = $resource->thumbsups()->firstOrCreate(['user_id' => $userId]);

        if ($thumbsup->wasRecentlyCreated) {
            $resource->increment('thumbsup_count');
            return $this->success(__('点赞成功'));
        } else {
            $thumbsup->delete();
            $resource->decrement('thumbsup_count');
            return $this->success(__('取消点赞'));
        }
    }





}
